$(function(){
    let userid = getCookie("userid")
    let token = getCookie("token");

   function init(){

    var options = {
        url:"http://127.0.0.1:8888/cart/list",
        type:"GET",
        data:{
            id:userid
        },
        headers:{
            authorization:token
        },
        dataType:"json"
          
    };

     $.ajax(options)
    .done(function(res){
       console.log(res);
       render(res);

})

   }

     function render(res){

          $(".cart-container").html(
              res.cart.map(
                  item=>`
                  <div class="goods"> 
                  <div class="p-goods">
                  <input type="checkbox" data-id=${item.goods_id}>
                    <div class="p-img">
                        <img src="${item.img_small_logo}" alt="">
                    </div>
                 
                      <div class="p-item">
                          ${item.title}
                      </div>
         
         
             </div>
         
               <div class="p-price">￥${item.price}</div>
         
              </div>
             <div class="p-quantity">
                 <div class="cartnum">
                     <button id="left" data-id="${item.goods_id}">-</button>
                     <input type="text" value="${item.cart_number}">
                     <button id="right" data-id="${item.goods_id}">+</button>
                 </div>
             </div>
             <div class="p-sum">￥${item.cart_number*item.price}</div>
                 <div class="p-options">
                     <a class="iconfont icon-lajitong" data-id=${item.goods_id} href="javascript:void(0)"></a>
                 </div>`
              ).join("")
          )

     };

     $(".cart-container").on("click" , "a" , remove )

    function remove(){
        // 1. 获取用户 id ; 
        var user_id  = getCookie("userid");
        // 2. 获取商品 id ; 
        var goods_id = $(this).attr("data-id");
            var options = {
                url : `http://127.0.0.1:8888/cart/remove` ,
                type : "GET",
                data : {
                      id : user_id , 
                      goodsId : goods_id
                },
                headers: {
                      authorization: token
                }
          }

          $.ajax(options)
          .done( function( data ) {
                 console.log( data );
                init()
          });
    }

      
    


    $(".cart-container").on("click" , "#left" , function(){
        let goods_nums = parseInt( $(this).next().val())-1;
        let goods_id = $(this).attr("data-id");
        add(goods_id,goods_nums)
    } );

    $(".cart-container").on("click" , "#right" , function(){
        let goods_nums = parseInt( $(this).prev().val())+1;
        let goods_id = $(this).attr("data-id");
        console.log(goods_id);
        add(goods_id,goods_nums)
    } )
         
       


    function add(sum,res){
     
        var user_id  = getCookie("userid");
    
         var options = {
                url : `http://127.0.0.1:8888/cart/number?id=${user_id}&goodsId=${sum}&number=${res}`,
                type : "POST",
                data : {
                      id : user_id , 
                      goodsId : sum,
                      number : res
                },
                headers: {
                      authorization: token
                }
          }

          $.ajax(options)
          .done( function( data ) {
                 console.log( data );
                init()
          });
    }



    $(".cart-container").on("change" , "input" , function(){
        var goods_id = $(this).attr("data-id");
       
             console.log(goods_id);
             choice(goods_id)
    } )

    function choice(num){
     
        var user_id  = getCookie("userid");
            

         var options = {
                url : `http://127.0.0.1:8888/cart/select?id=${user_id}&goodsId=${num}`,
                type : "POST",
                data : {
                      id : user_id , 
                      goodsId : num
                },
                headers: {
                      authorization: token
                }
          }

          $.ajax(options)
          .done( function( data ) {
                
          });
    }












init();

})